package com.gqz.aircraftwar.mvp.launch

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity
import androidx.databinding.DataBindingUtil
import com.gqz.aircraftwar.R
import com.gqz.aircraftwar.databinding.ActLaunchBinding
import com.gqz.aircraftwar.mvp.main.MainAct
import com.gqz.aircraftwar.mvp.test.TestAct
import com.gqz.aircraftwar.util.*

/**
 * Descraption
 *
 * @author gqz
 * @data 2020/3/20
 */
class LaunchAct : AppCompatActivity(), LaunchConstract.IView {

    private val TAG: String = "LaunchAct-->"

    private lateinit var binding: ActLaunchBinding
    private lateinit var presenter: LaunchConstract.IPresetner


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.act_launch)
        binding.launch = this
        LaunchPresenter(this)
    }

    override fun setIPresenter(presenter: LaunchConstract.IPresetner) {
        this.presenter = presenter
        lifecycle.addObserver(presenter)
        presenter.setBinding(binding)
    }

    fun onclick(v: View) {
        presenter?.loadPlayerInfo() ?: w(TAG, "presenter is null")
    }

    /**
     * 进入游戏主界面
     */
    override fun enterMain(bundle: Bundle?) {
        val intent = Intent(this, MainAct::class.java)
        intent.putExtra("player", bundle?.getString("player"))
        startActivity(intent)
    }

    var lastBackTime: Long = 0

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        var isExit: Boolean? = intent?.getBooleanExtra("exit", false)
        if (isExit != null && isExit) {
            finish()
        }
    }

    override fun onBackPressed() {
        var curBackTime: Long = System.currentTimeMillis()
        if (lastBackTime != 0L && curBackTime - lastBackTime < 1500) {
            finish()
        } else {
            toast(this, "再次点击退出程序")
            lastBackTime = curBackTime
        }
    }
}